package org.example.controller;

import jakarta.annotation.Resource;
import org.example.bean.User;
import org.example.service.UserService;
import org.example.util.JsonResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserCotroller {
    @Resource
    private UserService userService;

    @GetMapping
    public List<User> find(){
        return userService.list();
    }

    @GetMapping("/page/{page}")
    public JsonResult find(@PathVariable("page") int page){
        return userService.findUser(page);
    }
    @GetMapping("/id/{id}")
    public JsonResult findById(@PathVariable("id")String id){
        return userService.findUserById(id);
    }
    @GetMapping("/search/{username}/{page}")
    public JsonResult findUserByUsername(@PathVariable("username")String username,@PathVariable("page") int page){
        return userService.findUserByUsername(username,page);
    }

    @PostMapping
    public JsonResult save(User user){
        return userService.saveUser(user);
    }
    @PutMapping
    private JsonResult update(User user){
        return userService.updateUser(user);
    }
    @PutMapping("/change_pass/{id}/{pass}")
    public JsonResult changePass(@PathVariable("id")String id,@PathVariable("pass")String newpass){
        return userService.updateUserPassword(id,newpass);
    }
    @PutMapping("/change_status/{id}/{status}")
    public JsonResult update(@PathVariable("id")String id,@PathVariable("status")int status){
        return userService.updateUserStatus(id,status);
    }

    @GetMapping("/password_exists/{id}/{password}")
    public JsonResult CheckPasswordExists(@PathVariable("id")String id,@PathVariable("password")String password){
        return userService.checkPasswordExists(id,password);
    }

    @PostMapping("/login")
    public JsonResult login(User user){
        return userService.login(user);
    }

    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id")String id){
        return userService.removeUser(id);
    }
}
